package com.red.star.wechat.data.mappers;

import com.red.star.wechat.data.core.base.MyMapper;
import com.red.star.wechat.data.entity.Income;
import com.red.star.wechat.data.entity.vo.IncomeVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author nofish.yan@gmail.com
 * @date 2018/6/28.
 * 收益汇总数据访问接口
 */
public interface IncomeMapper extends MyMapper<Income> {

    @Select(value = "select * from t_income t where t.employee_id = #{income.employeeId} and " +
            "t.stored_card_id = #{income.storedCardId} and t.group_id = #{income.groupId} and " +
            "t.`status` = #{income.status}")
    Income findByEntity(@Param("income") Income income);

    @Select("SELECT * FROM t_income WHERE employee_id = #{employeeId} AND (status = 0 OR status = 1)")
    List<Income> listCurrentIncomeByEmployee(@Param("employeeId") Integer employeeId);

    List<IncomeVo> listIncome(@Param("vo") IncomeVo incomeVo);

    Integer countByCondition(@Param("idList") List<Integer> idList, @Param("status") Integer status);

    List<Income> findByCondition(@Param("idList") List<Integer> idList, @Param("status") Integer status);

    Integer batchUpdateStatus(@Param("list") List<Income> incomeList, @Param("status") Integer status);

    @Select(value = "select count(t.id) from t_income t left join t_employee te on t.employee_id = te.id where te.mall_code = #{mallCode} and t.`status` = #{status}")
    Integer countByStatus(@Param("mallCode") String mallCode, @Param("status") Integer status);

    Integer batchUpdateSettleDetailId();

    @Select("SELECT DISTINCT tsc.`name` FROM t_income ti " +
            "JOIN t_settle_detail tid ON ti.settle_detail_id " +
            "JOIN t_stored_card tsc ON ti.stored_card_id = tsc.id " +
            "WHERE tid.settle_id = #{settleId} AND tid.employee_id = #{employeeId}")
    List<String> listStoredCardBySettle(@Param("settleId") Integer settleId, @Param("employeeId") Integer employeeId);

    Integer updateStatusASettleDetailIdBySettleId(@Param("settleId") Integer settleId, @Param("status") Integer status);

    Integer updateStatusBySettleId(@Param("settleId") Integer settleId, @Param("status") Integer status);

    List<IncomeVo> findBySettleId(@Param("settleId") Integer settleId);

    Integer clearStatusBySettleId(@Param("storedCardIds") List<Integer> storedCardIds);

    @Select("SELECT ti.stored_card_id " +
            "FROM t_income ti " +
            "JOIN t_settle_detail tsd ON ti.settle_detail_id = tsd.id " +
            "WHERE tsd.settle_id = #{settleId} " +
            "GROUP BY ti.stored_card_id")
    List<Integer> listStoredCardIdBySettle(@Param("settleId") Integer settleId);
}
